package ac.il.technion.cs236369.hw5.core.servlets.projects;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;

import ac.il.technion.cs236369.hw5.core.entities.Project;
import ac.il.technion.cs236369.hw5.core.entities.User;


public enum ProjectParameters {
	PROJECT_NAME( "projectName"){
		public ProjectParameters saveParameterValue( Project project, HttpServletRequest request, EntityManager em) {
			project.setName( request.getParameter( this.toString( )));
			return this;
		}
	},
	PROJECT_DESCRIPTION( "projectDescription"){
		public ProjectParameters saveParameterValue( Project project, HttpServletRequest request, EntityManager em) {
			project.setDescription( request.getParameter( this.toString( )));
			return this;
		}
	},
	PROJECT_OWNER( "projectOwner") {
		public ProjectParameters saveParameterValue( Project project, HttpServletRequest request, EntityManager em) {
			User user = em.find( User.class, request.getUserPrincipal().getName( ));
			project.setUser(user); 
			return this;
		}
	},
	PROJECT_BUDGET( "projectBudget") {
		public ProjectParameters saveParameterValue( Project project, HttpServletRequest request, EntityManager em) {
			project.setBudget( Double.valueOf( request.getParameter( this.toString( ))));
			return this;
		}
	};
	
	private static final Map<String, ProjectParameters> parameters = new HashMap<String, ProjectParameters>( );
	static {
		for ( ProjectParameters parameter : ProjectParameters.values( )) {
			parameters.put( parameter.name().toUpperCase( ), parameter);
		}
	}
	
	private String parameterName;

	private ProjectParameters( String parameterName) {
		this.parameterName = parameterName;
	}
	
	static public ProjectParameters fromString( String name) {
		return parameters.get(name);
	}
	
	@Override
	public String toString() {
		return parameterName;
	}

	abstract public ProjectParameters saveParameterValue( Project project, HttpServletRequest request, EntityManager em);
}